C++ math
🗖 The math library in C++ offers a large number of functions for carrying out mathematical calculations. These functions, which are included in the
Commonly Used Math Functions in <cmath>
|
Function |
Description |
Example |
|
abs(x) |
Returns the
absolute value of x. |
abs(-5) → 5 |
|
sqrt(x) |
Returns the
square root of x. |
sqrt(16) → 4 |
|
pow(base,
exp) |
Returns base
raised to the power exp. |
pow(2, 3) → 8 |
|
sin(x) |
Returns the
sine of x (in radians). |
sin(3.14 / 2)
→ 1.0 |
|
cos(x) |
Returns the
cosine of x (in radians). |
cos(0) → 1.0 |
|
tan(x) |
Returns the
tangent of x (in radians). |
tan(0) → 0 |
|
log(x) |
Returns the
natural logarithm of x. |
log(1) → 0 |
|
log10(x) |
Returns the
base-10 logarithm of x. |
log10(100) →
2 |
|
exp(x) |
Returns e^x
(exponential function). |
exp(1) →
2.71828... |
|
ceil(x) |
Returns the
smallest integer ≥ x. |
ceil(3.2) → 4 |
|
floor(x) |
Returns the
largest integer ≤ x. |
floor(3.7) →
3 |
|
round(x) |
Rounds x to
the nearest integer. |
round(2.5) →
3 |
|
fmod(x, y) |
Returns the
remainder of x / y. |
fmod(7, 3) →
1 |
|
hypot(x, y) |
Returns
sqrt(x^2 + y^2). |
hypot(3, 4) →
5 |
Comments